package io.nutz.nutzsite.module.acm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;

import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.module.acm.models.Problem;
import io.nutz.nutzsite.module.acm.services.ContestProblemService;
import io.nutz.nutzsite.module.acm.services.ProblemService;
/**
 * acm题目 信息操作处理
 * 
 * @author lrw
 * @date 2020-02-16
 */
@IocBean
@At("/acm/problem")
public class ProblemController {
	private static final Log log = Logs.get();

	@Inject
	private ProblemService problemService;
	@Inject
	private ContestProblemService contestProblemService;
	
	@RequiresPermissions("acm:problem:view")
	@At("")
	@Ok("th:/acm/problem/problem.html")
	public void index(HttpServletRequest req) {

	}

	/**
	 * 查询acm题目列表
	 */
	@RequiresPermissions("acm:front:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("problemId") Integer problemId,
					   @Param("title") String title,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if(problemId!=null){
			cnd.and("problemId", "=", problemId);
		}
		else if (!Strings.isBlank(title)){
			cnd.and("title", "like", "%" + title +"%");
		}
		return problemService.tableList(pageNum,pageSize,cnd,null,null,null);
	}
	/**
	 * 适配双重列表
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	public List<Problem> list2(HttpServletRequest req) {
		
		return problemService.query("^(problemId|title)$", null);
	}
	/**
	 * 未选中的题目，适配ztree
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	public List<NutMap> list3(@Param("id") Integer id,HttpServletRequest req) {
		List<NutMap> listmap=new ArrayList<NutMap>();
		List<String> listpid=contestProblemService.getStrRowValues(Sqls.create("select problem_id from contest_problem where contest_id="+id));
		Cnd cnd=Cnd.NEW();
		if(listpid.size()>0) cnd.andNot("problemId","in",listpid);
		else cnd=null;
		List<Problem> listp=problemService.query("^(problemId|title)$", cnd);
		for(Problem p:listp){
			NutMap map=new NutMap();
			int pid=p.getProblemId();
			map.addv("id", pid);
			map.addv("name", pid+" "+p.getTitle());
			listmap.add(map);
		}
		return listmap;
	}
	/**
	 * 选中的题目，适配ztree
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	public List<NutMap> list32(@Param("id") Integer id,HttpServletRequest req) {
		List<NutMap> listmap=new ArrayList<NutMap>();
		List<String> listpid=contestProblemService.getStrRowValues(Sqls.create("select problem_id from contest_problem where contest_id="+id));
		Cnd cnd=Cnd.NEW();
		if(listpid.size()>0)
		{
			cnd.and("problemId","in",listpid);				
			List<Problem> listp=problemService.query("^(problemId|title)$", cnd);
			for(Problem p:listp){
				NutMap map=new NutMap();
				int pid=p.getProblemId();
				map.addv("id", pid);
				map.addv("name", pid+" "+p.getTitle());
				listmap.add(map);
			}
		}
		return listmap;
	}
	/**
	 * 新增acm题目
	 */
	@At("/add")
	@Ok("th:/acm/problem/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存acm题目
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:problem:add")
	@Slog(tag="acm题目", after="新增保存acm题目 id=${args[0].id}")
	public Object addDo(@Param("..") Problem problem,HttpServletRequest req) {
		try {
			problemService.insert(problem);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改acm题目
	 */
	@At("/edit/?")
	@Ok("th://acm/problem/edit.html")
	public void edit(String id, HttpServletRequest req) {
		Problem problem = problemService.fetch(Integer.parseInt(id));
		req.setAttribute("problem",problem);
	}

	/**
	 * 修改保存acm题目
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:problem:edit")
	@Slog(tag="acm题目", after="修改保存acm题目")
	public Object editDo(@Param("..") Problem problem,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(problem)){
				problemService.update(problem);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除acm题目
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("acm:problem:remove")
	@Slog(tag ="acm题目", after= "删除acm题目:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			problemService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
